package com.yisoo.ubag.product.service.impl;

import com.yisoo.common.utils.R;
import com.yisoo.ubag.product.vo.BrandAndSceneTreeVo;
import com.yisoo.ubag.product.vo.SceneTreeVo;
import com.yisoo.ubag.product.vo.TransferBrandDataVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.Query;

import com.yisoo.ubag.product.dao.ConBraSceDao;
import com.yisoo.ubag.product.entity.ConBraSceEntity;
import com.yisoo.ubag.product.service.ConBraSceService;


@Service("conBraSceService")
public class ConBraSceServiceImpl extends ServiceImpl<ConBraSceDao, ConBraSceEntity> implements ConBraSceService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ConBraSceEntity> page = this.page(
                new Query<ConBraSceEntity>().getPage(params),
                new QueryWrapper<ConBraSceEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<TransferBrandDataVo> getEnableBrandByCidAndSid(Long cid, Long sid) {
        QueryWrapper<ConBraSceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id",cid).eq("scene_id",sid);
        List<ConBraSceEntity> conBraSceEntityList = baseMapper.selectList(queryWrapper);
        return conBraSceEntityList.stream().map(brandEntity -> {
            return new TransferBrandDataVo(brandEntity.getBrandId(), brandEntity.getBrandName(), false);
        }).collect(Collectors.toList());
    }

    @Override
    public List<ConBraSceEntity> getHaveList(Long cid, Long sid) {
        QueryWrapper<ConBraSceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id",cid).eq("scene_id",sid);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public R getTree(Long cid) {
        QueryWrapper<ConBraSceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id",cid);
        List<ConBraSceEntity> allList = baseMapper.selectList(queryWrapper);
        List<BrandAndSceneTreeVo> tree = new ArrayList<>();
//收集出所有品牌，并新建出全部品牌树
        for (ConBraSceEntity temp : allList) {
            BrandAndSceneTreeVo brandAndSceneTreeVo = new BrandAndSceneTreeVo(temp.getBrandId(),temp.getBrandName(),null);
            if(!tree.contains(brandAndSceneTreeVo)){
                tree.add(brandAndSceneTreeVo);
            }
        }
        for(BrandAndSceneTreeVo temp:tree){
            temp.setChildren(new ArrayList<>());
        }
        for (BrandAndSceneTreeVo temp : tree) {
            for (ConBraSceEntity con : allList) {
                if(temp.getValue() == con.getBrandId()){
                    temp.setChildrenItem(new SceneTreeVo(con.getSceneId(),con.getSceneName()));
                }
            }
        }
        return R.ok().put("tree",tree);
    }


}